package com.mmy.service.scenic.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.dao.scenic.TScenicOrderRepository;
import com.mmy.dao.scenic.TScenicPlanRepository;
import com.mmy.dao.scenic.VScenicOrderRepository;
import com.mmy.dao.sys.TSysTogetherRepository;
import com.mmy.dao.sys.TSysUserRepository;
import com.mmy.domain.scenic.TScenicOrder;
import com.mmy.domain.scenic.TScenicPlan;
import com.mmy.domain.scenic.VScenicOrder;
import com.mmy.domain.sys.TSysTogether;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.ServiceCommon;
import com.mmy.service.scenic.ScenicOrderService;

@Service
public class ScenicOrderServiceImpl implements ScenicOrderService {
	@Autowired
	TScenicOrderRepository tScenicOrderRepository;
	@Autowired
	VScenicOrderRepository vScenicOrderRepository;
	@Autowired
	ServiceCommon common;
	@Autowired
	TScenicPlanRepository tScenicPlanRepository;
	@Autowired
	TSysUserRepository tSysUserRepository;
	@Autowired
	TSysTogetherRepository tSysTogetherRepository;

	/**
	 * 获取最大流水号
	 * 
	 * @return 返回值说明 流水号
	 * @author 创建人 dingxd
	 * @date 创建日期 2020.11.17
	 */
	public int getMaxID() {
		// 流水号
		Object objCode = tScenicOrderRepository.getMaxID();
		int iID = 0;
		if (objCode != null) {
			iID = Integer.parseInt(objCode.toString());
		}
		return iID;
	}

	/**
	 * 获取订单最大编码
	 * 
	 * @param 参数说明 计划单编码
	 * @return 返回值说明 订单编码
	 * @author 创建人 dingxd
	 * @date 创建日期 2020.11.17
	 */
	public String getMaxCode(int iPlanID) {
		// int 转 string
		String strPlanID = String.valueOf(iPlanID);
		// 返回订单最大编码
		Object objCode = tScenicOrderRepository.getMaxCode(strPlanID);
		String strCode = "";
		if (objCode != null) {
			strCode = objCode.toString();
		}
		return strCode;
	}

	/**
	 * 个人中心通过登录人获取景点门票预留 获取分页
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public HashMap<String, Integer> mapScenicReserve(String pageNumber, String pageSize, int iUserID) {
		Common common = new Common();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		mapPage = common.getPage(pageNumber, pageSize, vScenicOrderRepository.countByUserID(iUserID));
		return mapPage;
	}

	/**
	 * 个人中心通过登录人获取景点门票退单 获取分页
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public HashMap<String, Integer> mapScenicRefund(String pageNumber, String pageSize, int iUserID) {
		Common common = new Common();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		mapPage = common.getPage(pageNumber, pageSize, vScenicOrderRepository.countByRefundUserID(iUserID));
		return mapPage;
	}

	/**
	 * 个人中心通过登录人获取景点门票订单 获取分页
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public HashMap<String, Integer> mapScenicOrder(String pageNumber, String pageSize, int iUserID) {
		Common common = new Common();
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		mapPage = common.getPage(pageNumber, pageSize, vScenicOrderRepository.countByOrderUserID(iUserID));
		return mapPage;
	}

	/**
	 * 个人中心通过登录人获取景点门票预留
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public List<VScenicOrder> findByUserID(String pageNumber, String pageSize, int iUserID) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		List<VScenicOrder> lstScenicOrder = new ArrayList<VScenicOrder>();
		lstScenicOrder = vScenicOrderRepository.findByUserID(iUserID, iTempPageNo, iPageSize);
		return lstScenicOrder;
	}

	/**
	 * 个人中心通过登录人获取景点门票订单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public List<VScenicOrder> findByOrderUserID(String pageNumber, String pageSize, int iUserID) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		List<VScenicOrder> lstScenicOrder = new ArrayList<VScenicOrder>();
		lstScenicOrder = vScenicOrderRepository.findByOrderUserID(iUserID, iTempPageNo, iPageSize);
		return lstScenicOrder;

	}

	/**
	 * 个人中心通过登录人获取景点门票退单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public List<VScenicOrder> findByChargeBackUserID(String pageNumber, String pageSize, int iUserID) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		List<VScenicOrder> lstScenicOrder = new ArrayList<VScenicOrder>();
		lstScenicOrder = vScenicOrderRepository.findByChargeBackUserID(iUserID, iTempPageNo, iPageSize);
		return lstScenicOrder;

	}

	/**
	 * 个人中心通过登录人取消预留
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateState(int iID, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		TScenicOrder dbScenicOrder = tScenicOrderRepository.findByID(iID);
		dbScenicOrder.setOrderState((byte) 3);
		dbScenicOrder.setModifyDate(new Date());
		dbScenicOrder.setReviser(sessionSysUser.getUserName());
		tScenicOrderRepository.save(dbScenicOrder);
	}

	/**
	 * 个人中心通过登录人申请景点退单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateRefundState(int iID, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		TScenicOrder dbScenicOrder = tScenicOrderRepository.findByID(iID);
		dbScenicOrder.setOrderState((byte) 4);
		dbScenicOrder.setRefundProcess((byte) 1);
		dbScenicOrder.setModifyDate(new Date());
		dbScenicOrder.setReviser(sessionSysUser.getUserName());
		tScenicOrderRepository.save(dbScenicOrder);
	}

	/**
	 * 个人中心通过登录人点击撤销景点退单
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public void updateCancelState(int iID, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		TScenicOrder dbScenicOrder = tScenicOrderRepository.findByID(iID);
		dbScenicOrder.setOrderState((byte) 2);
		dbScenicOrder.setRefundProcess((byte) 0);
		dbScenicOrder.setModifyDate(new Date());
		dbScenicOrder.setReviser(sessionSysUser.getUserName());
		tScenicOrderRepository.save(dbScenicOrder);
	}

	/**
	 * 通过订单ID查询
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 */
	public TScenicOrder findByID(int iID) {
		// TODO Auto-generated method stub
		return tScenicOrderRepository.findByID(iID);
	}

	/**
	 * 后台购票信息查看 超级管理员登录查看景区订单时用到 全部
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2021.01.25
	 */
	public List<VScenicOrder> findBySeachAdministrator(String pageNumber, String pageSize, String strScenicName,
			String strOrderName, String strOrderTel, String strOrderDate, String strOrderState) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		List<Integer> lstOrderState = new ArrayList<Integer>();
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		if (strOrderState == null || strOrderState == "") {
			lstOrderState.add(1);
			lstOrderState.add(2);
			lstOrderState.add(4);
		} else if (strOrderState.equals("2")) {
			lstOrderState.add(1);
			lstOrderState.add(2);
		} else {
			lstOrderState.add(4);
		}

		List<VScenicOrder> lstScenicOrder = vScenicOrderRepository.findBySeachAdministrator(strScenicName, strOrderName,
				strOrderTel, strOrderDate, lstOrderState, iTempPageNo, iPageSize);

		return lstScenicOrder;
	}

	/**
	 * 后台购票信息查看 超级管理员登录查看景区订单时用到 全部
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2021.01.25
	 */
	public HashMap<String, Integer> mapSeachAdministrator(String pageNumber, String pageSize, String strScenicName,
			String strOrderName, String strOrderTel, String strOrderDate, String strOrderState) {
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		List<Integer> lstOrderState = new ArrayList<Integer>();
		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		if (strOrderState == null || strOrderState == "") {
			lstOrderState.add(1);
			lstOrderState.add(2);
			lstOrderState.add(4);
		} else if (strOrderState.equals("2")) {
			lstOrderState.add(1);
			lstOrderState.add(2);
		} else {
			lstOrderState.add(4);
		}
		mapPage = common.getPage(pageNumber, pageSize, vScenicOrderRepository.countBySeachAdministrator(strScenicName,
				strOrderName, strOrderTel, strOrderDate, lstOrderState));
		return mapPage;
	}

	/**
	 * 后台非管理员 查看景区订单时用到 全部
	 *
	 * @author 创建人 hanxl
	 * @param strOrderState
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2020.12.29
	 */
	public List<VScenicOrder> findBySearchOperator(String pageNumber, String pageSize, String strScenicName,
			String strOrderName, String strOrderTel, String strOrderDate, String strOrderState,
			TSysUser sessionSysUser) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		List<Integer> lstOrderState = new ArrayList<Integer>();
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		if (strOrderState == null || strOrderState == "") {
			lstOrderState.add(1);
			lstOrderState.add(2);
			lstOrderState.add(4);
		} else if (strOrderState.equals("2")) {
			lstOrderState.add(1);
			lstOrderState.add(2);
		} else {
			lstOrderState.add(4);
		}
		int iOrgID = sessionSysUser.getOrgID();
		List<VScenicOrder> lstScenicOrder = vScenicOrderRepository.findBySearchOperator(strScenicName, strOrderName,
				strOrderTel, strOrderDate, lstOrderState, iOrgID, iTempPageNo, iPageSize);

		return lstScenicOrder;

	}

	/**
	 * 后台非管理员登录 查看景区订单时用到 分页 全部
	 *
	 * @author 创建人 hanxl
	 * @param strOrderState
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2020.12.29
	 */
	public HashMap<String, Integer> mapSearchOperator(String pageNumber, String pageSize, String strScenicName,
			String strOrderName, String strOrderTel, String strOrderDate, String strOrderState,
			TSysUser sessionSysUser) {
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		List<Integer> lstOrderState = new ArrayList<Integer>();
		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		if (strOrderState == null || strOrderState == "") {
			lstOrderState.add(1);
			lstOrderState.add(2);
			lstOrderState.add(4);
		} else if (strOrderState.equals("2")) {
			lstOrderState.add(1);
			lstOrderState.add(2);
		} else {
			lstOrderState.add(4);
		}
		int iOrgID = sessionSysUser.getOrgID();

		mapPage = common.getPage(pageNumber, pageSize, vScenicOrderRepository.countBySearchOperator(strScenicName,
				strOrderName, strOrderTel, strOrderDate, lstOrderState, iOrgID));

		return mapPage;
	}

	/**
	 * 后台购票信息查看 超级管理员登录查看景区订单时用到 待入园
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2021.01.25
	 */
	public List<VScenicOrder> findBySeachAdministratorUnUsed(String pageNumber, String pageSize, String strScenicName,
			String strOrderName, String strOrderTel, String strOrderDate) {
		int iPageSize = 0;
		int iTempPageNo = 0;

		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		List<VScenicOrder> lstScenicOrder = vScenicOrderRepository.findBySeachAdministratorUnUsed(strScenicName,
				strOrderName, strOrderTel, strOrderDate, iTempPageNo, iPageSize);

		return lstScenicOrder;
	}

	/**
	 * 后台购票信息查看 超级管理员登录查看景区订单时用到 待使用
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2021.01.25
	 */
	public HashMap<String, Integer> mapSeachAdministratorUnUsed(String pageNumber, String pageSize,
			String strScenicName, String strOrderName, String strOrderTel, String strOrderDate) {
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();

		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		mapPage = common.getPage(pageNumber, pageSize, vScenicOrderRepository
				.countBySeachAdministratorUnUsed(strScenicName, strOrderName, strOrderTel, strOrderDate));
		return mapPage;
	}

	/**
	 * 后台非超级管理员 查看景区订单时用到 未入园待使用
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2020.12.29
	 */
	public List<VScenicOrder> findBySearchOperatorUnUsed(String pageNumber, String pageSize, String strScenicName,
			String strOrderName, String strOrderTel, String strOrderDate, TSysUser sessionSysUser) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		int iOrgID = sessionSysUser.getOrgID();
		List<VScenicOrder> lstScenicOrder = vScenicOrderRepository.findBySearchOperatorUnUsed(strScenicName,
				strOrderName, strOrderTel, iOrgID, strOrderDate, iTempPageNo, iPageSize);

		return lstScenicOrder;
	}

	/**
	 * 后台非超级管理员 查看景区订单时用到 分页 未入园待使用
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2020.12.29
	 */
	public HashMap<String, Integer> mapSearchOperatorUnUsed(String pageNumber, String pageSize, String strScenicName,
			String strOrderName, String strOrderTel, String strOrderDate, TSysUser sessionSysUser) {
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		int iOrgID = sessionSysUser.getOrgID();
		mapPage = common.getPage(pageNumber, pageSize, vScenicOrderRepository.countBySearchOperatorUnUsed(strScenicName,
				strOrderName, strOrderTel, iOrgID, strOrderDate));
		return mapPage;
	}

	/**
	 * 后台购票信息查看 超级管理员登录查看景区订单时用到 近期待入园
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2021.01.27
	 */
	public List<VScenicOrder> findBySeachAdministratorRecentlyUse(String pageNumber, String pageSize,
			String strScenicName, String strOrderName, String strOrderTel, int iMaxDay, String strOrderDate) {
		int iPageSize = 0;
		int iTempPageNo = 0;

		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		List<VScenicOrder> lstScenicOrder = vScenicOrderRepository.findBySeachAdministratorRecentlyUse(strScenicName,
				strOrderName, strOrderTel, iMaxDay, strOrderDate, iTempPageNo, iPageSize);

		return lstScenicOrder;
	}

	/**
	 * 后台购票信息查看 超级管理员登录查看景区订单时用到近期待入园
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2021.01.27
	 */
	public HashMap<String, Integer> mapSeachAdministratorRecentlyUse(String pageNumber, String pageSize,
			String strScenicName, String strOrderName, String strOrderTel, int iMaxDay, String strOrderDate) {
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();

		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		mapPage = common.getPage(pageNumber, pageSize, vScenicOrderRepository
				.countBySeachAdministratorRecentlyUse(strScenicName, strOrderName, strOrderTel, iMaxDay, strOrderDate));
		return mapPage;
	}

	/**
	 * 后台非超级管理员 查看景区订单时用到 近期待使用
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2020.12.29
	 */
	public List<VScenicOrder> findBySearchOperatorRecentlyUse(String pageNumber, String pageSize, String strScenicName,
			String strOrderName, String strOrderTel, TSysUser sessionSysUser, int iMaxDay, String strOrderDate) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		int iOrgID = sessionSysUser.getOrgID();
		List<VScenicOrder> lstScenicOrder = vScenicOrderRepository.findBySearchOperatorRecentlyUse(strScenicName,
				strOrderName, strOrderTel, iOrgID, iMaxDay, strOrderDate, iTempPageNo, iPageSize);

		return lstScenicOrder;
	}

	/**
	 * 后台非超级管理员 查看景区订单时用到 分页 近期待使用
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2020.12.29
	 */
	public HashMap<String, Integer> mapSearchOperatorRecentlyUse(String pageNumber, String pageSize,
			String strScenicName, String strOrderName, String strOrderTel, TSysUser sessionSysUser, int iMaxDay,
			String strOrderDate) {
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		int iOrgID = sessionSysUser.getOrgID();
		mapPage = common.getPage(pageNumber, pageSize, vScenicOrderRepository.countBySearchOperatorRecentlyUse(
				strScenicName, strOrderName, strOrderTel, iOrgID, iMaxDay, strOrderDate));
		return mapPage;
	}

	/**
	 * 后台购票信息查看 超级管理员登录查看景区订单时用到 已使用
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2021.01.25
	 */
	public List<VScenicOrder> findBySeachAdministratorUsed(String pageNumber, String pageSize, String strScenicName,
			String strOrderName, String strOrderTel, String strOrderDate) {
		int iPageSize = 0;
		int iTempPageNo = 0;

		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		List<VScenicOrder> lstScenicOrder = vScenicOrderRepository.findBySeachAdministratorUsed(strScenicName,
				strOrderName, strOrderTel, strOrderDate, iTempPageNo, iPageSize);

		return lstScenicOrder;
	}

	/**
	 * 后台购票信息查看 超级管理员登录查看景区订单时用到 已使用
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2021.01.25
	 */
	public HashMap<String, Integer> mapSeachAdministratorUsed(String pageNumber, String pageSize, String strScenicName,
			String strOrderName, String strOrderTel, String strOrderDate) {
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();

		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		mapPage = common.getPage(pageNumber, pageSize, vScenicOrderRepository
				.countBySeachAdministratorUsed(strScenicName, strOrderName, strOrderTel, strOrderDate));
		return mapPage;
	}

	/**
	 * 后台非超级管理员 查看景区订单时用到 已使用
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2020.12.29
	 */
	public List<VScenicOrder> findBySearchOperatorUsed(String pageNumber, String pageSize, String strScenicName,
			String strOrderName, String strOrderTel, String strOrderDate, TSysUser sessionSysUser) {
		int iPageSize = 0;
		int iTempPageNo = 0;
		if (pageNumber == null) {
			pageNumber = "1";
		}
		if (pageSize == null) {
			iPageSize = 20;
		} else {
			iPageSize = Integer.valueOf(pageSize);
			iTempPageNo = (Integer.valueOf(pageNumber) - 1) * iPageSize;
		}
		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		int iOrgID = sessionSysUser.getOrgID();
		List<VScenicOrder> lstScenicOrder = vScenicOrderRepository.findBySearchOperatorUsed(strScenicName, strOrderName,
				strOrderTel, iOrgID, strOrderDate, iTempPageNo, iPageSize);

		return lstScenicOrder;
	}

	/**
	 * 后台非超级管理员 查看景区订单时用到 分页 已使用
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2020.12.29
	 */
	public HashMap<String, Integer> mapSearchOperatorUsed(String pageNumber, String pageSize, String strScenicName,
			String strOrderName, String strOrderTel, String strOrderDate, TSysUser sessionSysUser) {
		HashMap<String, Integer> mapPage = new HashMap<String, Integer>();
		if (strScenicName == null) {
			strScenicName = "";
		}
		if (strOrderName == null) {
			strOrderName = "";
		}
		if (strOrderTel == null) {
			strOrderTel = "";
		}
		if (strOrderDate == null) {
			strOrderDate = "";
		}
		int iOrgID = sessionSysUser.getOrgID();
		mapPage = common.getPage(pageNumber, pageSize, vScenicOrderRepository.countBySearchOperatorUsed(strScenicName,
				strOrderName, strOrderTel, iOrgID, strOrderDate));
		return mapPage;
	}

	/**
	 * 未使用申请退单的订单 退款
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2020.12.29
	 */
	public TScenicOrder updateRefund(TScenicOrder tScenicOrder, TSysUser sessionSysUser) {
		// 通过流水号ID查询景点电子票订单
		TScenicOrder dbScenicOrder = tScenicOrderRepository.findByID(tScenicOrder.getID());
		// 将退单流程和退单金额填写
		dbScenicOrder.setOrderState((byte) 4);
		dbScenicOrder.setSpare2(String.valueOf(2));
		dbScenicOrder.setRefund(tScenicOrder.getRefund());
		dbScenicOrder.setRefundProcess(tScenicOrder.getRefundProcess());
		dbScenicOrder.setModifyDate(new Date());
		dbScenicOrder.setReviser(sessionSysUser.getUserName());
		tScenicOrderRepository.save(dbScenicOrder);
		return dbScenicOrder;
	}

	/**
	 * 前台景区电子票创建订单
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2020.12.29
	 */
	public Map<String, Object> createOrderScenic(String strScenicPlan, String strScenicOrder, String strUseDate,
			TSysUser sessionSysUser) {
		// 处理order
		List<TScenicOrder> lstScenicOrder = JSON.parseArray(strScenicOrder, TScenicOrder.class);

		// 处理date
		Calendar cal = Calendar.getInstance();
		String date = strUseDate.replace("-", "");
		Integer year = Integer.parseInt(date.substring(0, 4));
		Integer month = Integer.parseInt(date.substring(4, 6));
		Integer day = Integer.parseInt(date.substring(6, 8));
		cal.set(year, month - 1, day, 23, 59, 59);
		Date useDate = cal.getTime();

		// 获取总额
		BigDecimal total = new BigDecimal("0");
		Integer orderNumber = new Integer(0);
		List<TScenicPlan> lstScenicPlan = JSON.parseArray(strScenicPlan, TScenicPlan.class);
		for (TScenicPlan tScenicPlan : lstScenicPlan) {
			TScenicPlan dbScenicPlan = tScenicPlanRepository.findByPlanID(tScenicPlan.getPlanID());
			BigDecimal price = dbScenicPlan.getPrice();
			Integer num = tScenicPlan.getNumber();
			// 订单根据预留数量减少
			int count = dbScenicPlan.getNumber() - num;
			dbScenicPlan.setNumber(count);
			dbScenicPlan.setModifyDate(new Date());
			dbScenicPlan.setReviser(sessionSysUser.getUserName());
			tScenicPlanRepository.save(dbScenicPlan);

			orderNumber = orderNumber + num;
			BigDecimal sum = price.multiply(BigDecimal.valueOf(num));
			total = total.add(sum);
		}
		Map<String, Object> data = new HashMap<>();
		List<Integer> lstID = new ArrayList<Integer>();
		for (TScenicOrder tScenicOrder : lstScenicOrder) {
			int iPlanID = Integer.valueOf(tScenicOrder.getOrderID());
			// 获取 流水号
			tScenicOrder.setID(common.createMaxID(UtilConstants.TableName.T_SCENIC_ORDER));
			// 获取 订单号
			String cOrderID = common.createOrderCode(UtilConstants.TableName.T_SCENIC_ORDER, iPlanID);
			tScenicOrder.setOrderID(cOrderID);
			// 订单ID已存在=planID
			// 预留操作人
			tScenicOrder.setReserveUserID(sessionSysUser.getUserID());
			// 预留人姓名
			tScenicOrder.setReserveName(sessionSysUser.getUserName());
			// 预留人手机号
			tScenicOrder.setReserveTel(sessionSysUser.getUserMobile());
			// 预留日期
			tScenicOrder.setReserveDate(new Date());
			// 订单操作人
			tScenicOrder.setOrderUserID(sessionSysUser.getUserID());
			// 订单日期
			tScenicOrder.setOrderDate(new Date());
			// 订单人姓名、订单人电话、身份证号已存在
			// 同行人问题
			if (sessionSysUser.getUserType() == (byte) 0) {

				// 第一步查询身份证号码是否存在
				TSysUser dbSysUser = tSysUserRepository.findByIdentity(tScenicOrder.getIdentity());
				// 获得用户ID
				TSysUser tSysUser = new TSysUser();
				int iUserID = 0;
				// 如果等于空 在用户表里创建该用户
				if (dbSysUser == null) {

					tSysUser.setUserID(common.createBaseCode(UtilConstants.TableName.T_SYS_USER));
					// 默认账号和密码
					String strUserAccount = "visitor" + tSysUser.getUserID();
					tSysUser.setUserAccount(strUserAccount);
					tSysUser.setPassWord("123456");
					tSysUser.setUserName(tScenicOrder.getOrderName());
					tSysUser.setIdentity(tScenicOrder.getIdentity());
					// 用户推荐人为登录人
					tSysUser.setUpUserID(sessionSysUser.getUserID());
					// 用户所属公司为登陆人所属公司
					tSysUser.setOrgID(sessionSysUser.getOrgID());
					tSysUser.setCreator(sessionSysUser.getUserName());
					tSysUser.setCreateDate(new Date());
					tSysUserRepository.save(tSysUser.getUserID());
					tSysUserRepository.save(tSysUser);
					// 如果为空 获取创建的用户的ID
					iUserID = tSysUser.getUserID();
				} else {

					// 如果不为空，获得用户的ID
					iUserID = dbSysUser.getUserID();
				}

				// 查询同行人表中是否已存在该出行人和同行人
				TSysTogether dbSysTogether = tSysTogetherRepository.findByUserIDAndPeerID(sessionSysUser.getUserID(),
						iUserID);
				// 如果为空 创建同行人表
				if (dbSysTogether == null) {
					// 获得用户ID
					TSysTogether tSysTogether = new TSysTogether();

					tSysTogether.setID(common.createMaxID(UtilConstants.TableName.T_SYS_TOGETHER));
					tSysTogether.setUserID(sessionSysUser.getUserID());
					tSysTogether.setPeerID(iUserID);

					tSysTogether.setCreator(sessionSysUser.getUserName());
					tSysTogether.setCreateDate(new Date());
					tSysTogetherRepository.save(tSysTogether.getID());
					tSysTogetherRepository.save(tSysTogether);
				}
			}
			// 订单状态为1 正式订单
			tScenicOrder.setOrderState((byte) 1);
			// 订单数量
			tScenicOrder.setOrderNumber(orderNumber);
			// 订单总额
			tScenicOrder.setOrderAmount(total);
			// 订单使用日期
			tScenicOrder.setUseDate(useDate);
			// 订单底价、同业价、售价 从计划里获取
			TScenicPlan dbScenicPlan = tScenicPlanRepository.findByPlanID(iPlanID);
			tScenicOrder.setFloor(dbScenicPlan.getFloor());
			tScenicOrder.setPeer(dbScenicPlan.getPeer());
			tScenicOrder.setPrice(dbScenicPlan.getPrice());
			// 创建登录人登录时间
			tScenicOrder.setCreator(sessionSysUser.getUserName());
			tScenicOrder.setCreateDate(new Date());
			// 创建订单
			tScenicOrderRepository.save(tScenicOrder.getID());
			tScenicOrderRepository.save(tScenicOrder);
			lstID.add(tScenicOrder.getID());
		}
		data.put("lstID", lstID);
		return data;
	}

	/**
	 * 函数功能说明：收据提交
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 2021.01.12
	 */
	public List<TScenicOrder> updateScenicOrder(TScenicOrder tScenicOrder, TSysUser sessionSysUser, String lstID) {
		// 拆分字符串
		String[] arrID = lstID.split(",");
		List<TScenicOrder> lstScenicOrder = new ArrayList<TScenicOrder>();
		for (int i = 0; i < arrID.length; i++) {
			// 定义一个新的实体类对象
			TScenicOrder dbScenicOrder = tScenicOrderRepository.findByID(Integer.parseInt(arrID[i]));
			dbScenicOrder.setPayType(tScenicOrder.getPayType());

			dbScenicOrder.setReceivable(tScenicOrder.getReceivable());
			dbScenicOrder.setVoucher(tScenicOrder.getVoucher());
			dbScenicOrder.setReceipt(tScenicOrder.getReceipt());
			if (tScenicOrder.getReceivable() == tScenicOrder.getReceipt()) {
				dbScenicOrder.setOrderState((byte) 2);
			}
			dbScenicOrder.setModifyDate(new Date());
			dbScenicOrder.setReviser(sessionSysUser.getUserName());
			tScenicOrderRepository.save(dbScenicOrder);
			lstScenicOrder.add(dbScenicOrder);
		}
		return lstScenicOrder;
	}

	/**
	 * * 函数功能说明： 后台未入园的的订单 退单流程改为申请退单
	 *
	 * 登录人所属公司与景区所属公司不相等 操作人与登录人所属公司相等
	 * 
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl 修改人hanxl
	 * @date 创建日期 修改时间2021.01.06
	 */
	public Map<String, String> updateProcess(int iID, TSysUser sessionSysUser) {
		TScenicOrder dbScenicOrder = tScenicOrderRepository.findByID(iID);
		dbScenicOrder.setRefundProcess((byte) 1);
		dbScenicOrder.setModifyDate(new Date());
		dbScenicOrder.setReviser(sessionSysUser.getUserName());
		tScenicOrderRepository.save(dbScenicOrder);
		Map<String, String> map = new HashMap<>();
		map.put("strOrderName", dbScenicOrder.getOrderName());
		return map;
	}

	/**
	 * 未使用申请退单的订单 退款
	 *
	 * @author 创建人 hanxl
	 * @param strOrderTel
	 * @param strOrderName
	 * @date 创建日期 2020.12.29
	 */
	public void updateRefundAndProcess(TScenicOrder tScenicOrder, TSysUser sessionSysUser) {
		// 通过流水号ID查询景点电子票订单
		TScenicOrder dbScenicOrder = tScenicOrderRepository.findByID(tScenicOrder.getID());
		// 将退单流程和退单金额填写
		dbScenicOrder.setRefund(tScenicOrder.getRefund());
		dbScenicOrder.setRefundProcess(tScenicOrder.getRefundProcess());
		dbScenicOrder.setModifyDate(new Date());
		dbScenicOrder.setReviser(sessionSysUser.getUserName());
		tScenicOrderRepository.save(dbScenicOrder);

	}

	/**
	 * 函数功能说明：后台显示已使用的订单信息 结账
	 *
	 * @param 参数说明 tScenicOrder
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2021.01.28
	 */
	public void updateRemittanceStateScenic(String checkList, TSysUser sessionSysUser) {
		String[] checklist = checkList.split(",");
		for (String str : checklist) {
			int iID = Integer.parseInt(str);
			TScenicOrder dbScenicOrder = tScenicOrderRepository.findByID(iID);
			dbScenicOrder.setRemittanceState((byte) 1);
			dbScenicOrder.setModifyDate(new Date());
			dbScenicOrder.setReviser(sessionSysUser.getUserName());
			tScenicOrderRepository.save(dbScenicOrder);
		}
	}

	/**
	 * 函数功能说明：后台显示订单详情
	 *
	 * @param 参数说明 vScenicOrder
	 * @return 返回值说明 jsp页面
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.01.28
	 */
	public VScenicOrder findByID(String strID) {
		int iID = Integer.parseInt(strID);

		return vScenicOrderRepository.findByID(iID);
	}

}
